<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use app\common\model\Config as ConfigModel;

/**
 * 假数据统计
 *
 * @icon fa fa-circle-o
 */
class Fstatistics extends Backend
{

    /**
     * Fstatistics模型对象
     * @var \app\admin\model\Fstatistics
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Fstatistics;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
     /**
     * 导入
     *
     * @return void
     * @throws PDOException
     * @throws BindParamException
     */
    public function import()
    {
        $config_where['name'] = 'cdnurl';

        $url = ConfigModel::where($config_where)->value('value');
        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        if(strstr($file, $url))
        {
            $file = str_replace($url,'',$file);
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        //实例化reader
        $ext = pathinfo($filePath, PATHINFO_EXTENSION);
        if (!in_array($ext, ['xls', 'xlsx'])) {
            $this->error(__('Unknown data format'));
        }
        if ($ext === 'xls') {
            $reader = new Xls();
        } else {
            $reader = new Xlsx();
        }

        if (!$PHPExcel = $reader->load($filePath)) {
            $this->error(__('Unknown data format'));
        }
        $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        $datas = [];
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $data['user_name'] = $currentSheet->getCellByColumnAndRow(1, $currentRow)->getValue();
            $data['user_card_count'] = $currentSheet->getCellByColumnAndRow(2, $currentRow)->getValue();
            $data['user_active_card_count'] = $currentSheet->getCellByColumnAndRow(3, $currentRow)->getValue();
            $data['travel_count'] = $currentSheet->getCellByColumnAndRow(4, $currentRow)->getValue();
            $data['total_profit'] = $currentSheet->getCellByColumnAndRow(5, $currentRow)->getValue();
            $data['agent_profit'] = $currentSheet->getCellByColumnAndRow(6, $currentRow)->getValue();
            $data['travel_profit'] = $currentSheet->getCellByColumnAndRow(7, $currentRow)->getValue();
            $data['user_money'] = $currentSheet->getCellByColumnAndRow(8, $currentRow)->getValue();
            $data['with_money'] = $currentSheet->getCellByColumnAndRow(9, $currentRow)->getValue();
            
            if($data['user_name'] )
            {
                $datas[] = $data;
            }

        }
        if(count($datas) == 0)
        {
            $this->error('数据不能为空');
        }
        //halt($datas);
        //加载文件
        $rank_data = [];
        $is_success = 0;
        try {
            $u_status = true;
            foreach($datas as $k => $v)
            {
               
                $this->model->insertGetId($v);
                $is_success ++;
            }
        } catch (Exception $exception) {
            $this->error("导入失败");
        }
        $this->success("导入成功，导入".$is_success.'条数据');
    }

}
